package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.config.ConstantConfig;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.entity.Course;
import com.wzx.data.entity.Teacher;
import com.wzx.data.utils.FileUtils;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.CourseWare;
import com.wzx.data.service.ICourseWareService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzx.data.vo.CourseVo;
import com.wzx.data.vo.CourseWareVo;
import com.wzx.data.vo.TeacherVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 课件 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "课件管理接口")
@RequestMapping("/wzx/courseWare")
@Transactional
public class CourseWareController {

    @Autowired
    private ICourseWareService iCourseWareService;


    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条课件")
    public Result<CourseWare> get(@RequestParam String id){
        return new ResultUtil<CourseWare>().setData(iCourseWareService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部课件个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iCourseWareService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部课件")
    public Result<List<CourseWare>> getAll(){
        return new ResultUtil<List<CourseWare>>().setData(iCourseWareService.list());
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result page(@ModelAttribute CourseWareVo vo, @ModelAttribute PageVo pageVo){
        System.out.println(vo);
        final LambdaQueryWrapper<CourseWare> lambda = new QueryWrapper<CourseWare>().lambda();
        this.buildCondition(lambda,vo);
        final IPage<CourseWare> page = iCourseWareService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), lambda);
        PageInfo<CourseWare> pageInfo = new PageInfo<>(pageVo.getPageNumber(), pageVo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        return ResultUtil.data(pageInfo);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改课件")
    public Result<CourseWare> saveOrUpdate(CourseWare courseWare){
        System.out.println("CourseWare: "+courseWare);
        if(courseWare.getId() != null){
            iCourseWareService.updateById(courseWare);
            return ResultUtil.success("修改成功");
        }
        if(iCourseWareService.save(courseWare)){
            return ResultUtil.success("保存成功");
        }
        return ResultUtil.error();
    }


    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增课件")
    public Result<CourseWare> insert(CourseWare courseWare){
        iCourseWareService.saveOrUpdate(courseWare);
        return new ResultUtil<CourseWare>().setData(courseWare);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑课件")
    public Result<CourseWare> update(CourseWare courseWare){
        iCourseWareService.saveOrUpdate(courseWare);
        return new ResultUtil<CourseWare>().setData(courseWare);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除课件")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iCourseWareService.deleteFile(id);
        }
        return ResultUtil.success();
    }
    /**
     * 构建查询条件
     * @param lambda
     * @param vo
     */
    private void buildCondition(LambdaQueryWrapper<CourseWare> lambda, CourseWareVo vo) {
        if(vo==null) {
            return;
        }
        if (ObjectUtils.isNotEmpty(vo.getFileName())){
            lambda.like(CourseWare::getFileName, vo.getFileName());
        }
        if (ObjectUtils.isNotEmpty(vo.getCourseId())){
            lambda.like(CourseWare::getCourseId, vo.getCourseId());
        }
        if (ObjectUtils.isNotEmpty(vo.getTeacherId())){
            lambda.like(CourseWare::getTeacherId, vo.getTeacherId());
        }
        //根据startTime
        if (ObjectUtils.isNotEmpty(vo.getStartTime())){
            lambda.ge(CourseWare::getCreateTime, vo.getStartTime());
        }
        //根据endTime
        if (ObjectUtils.isNotEmpty(vo.getEndTime())){
            lambda.le(CourseWare::getCreateTime, vo.getEndTime());
        }
        lambda.eq(CourseWare::getDelFlag,0);
        lambda.orderByDesc(CourseWare::getCreateTime);
    }

}
